function ctrlInitArrowMediaConnector(context,selector,data){
	if( $(context).is(selector) )
		var context = $(selector);
	else
		var context = $(selector, context);
	
	if(data.initJsCallback && window[data.initJsCallback] ){
		var find = context.find(".ctrl-media-elements-count");
		if(find.length){
			var text = find.text();
			try{
				window[data.initJsCallback]( parseInt( text ) );
			}catch(e){
				alert("Media connector init callback error: \n" + e);
			}
		}
	}
	
	
	$(".ctrl-media-connector-files", context).sortable({
		 axis: 'y',
		 handle: '.ctrl-media-connector-file-move',
		 stop: function(event, ui) {
			 	var currId = $(ui.item).attr("connectionId" );
			 	
			 	var prev = $(ui.item).prev().attr("connectionId" );
			 	prev = (typeof(prev) != "undefined")?prev:-1;
			 	var next = $(ui.item).next().attr("connectionId" );
			 	next = (typeof(next) != "undefined")?next:-1;
			 	
				ArrowDataConnector.doAction(
						'media.MediaElementConnection', 
						currId,
						"move", 
						{
							prev:  prev,
							next: next
						}, 
						{}, 
						function(response){
							//CtrlAjax.changeStateVars($(ui.item),{},{});
						}
				);
		 }
	});
	
	$(".ctrl-media-connector-file-rename", context).click(function(){
		var elId = $(this).attr("href" );
		var titleCell = $(this).parent().parent().find(".ctrl-media-connector-file-title");
		
		var popupText =  $('<input type="text" style="width: 300px;" />').val( titleCell.find("span").text());
		
		popupIn = $('<div>  <a href="" class="ctrl-media-connector-submit">&nbsp;</a><a class="ctrl-media-connector-cancel" >&nbsp;</a></div>').prepend(popupText);
		var popup = titleCell.popup({
			el: popupIn, 
			dType: "leftTop",
			open: true
		});
		
		popupIn.find(".ctrl-media-connector-cancel").click(function(){
			popup.remove();
			return false;
		});
		
		popupIn.find(".ctrl-media-connector-submit").click(function(){
			if(popupText.val() == ""){
				alert("Podaj nazwę");
				return false;
			}
			ArrowDataConnector.saveObjectData(
					'media.MediaElement',
					elId,
					{"name": popupText.val()},
					{},
					function(data){
						CtrlAjax.changeStateVars(titleCell,{},{});
					}
			);
			popup.remove();
			return false;
		});
		popupText.keyup(function(event){
			if(event.keyCode == 13) popupIn.find(".ctrl-media-connector-submit").click().click()
		});
		popupText.focus();
		return false;
	});
	
	$(".ctrl-media-connector-file-change-description", context).click(function(){
		var elId = $(this).attr("href" );
		var titleCell = $(this).parent().parent().find(".ctrl-media-connector-file-description");
		
		var popupText =  $('<textarea style="width: ' +(titleCell.width())+ 'px; height: 50px;" ></textarea>').val( titleCell.find("span").text());
		
		popupIn = $('<div >  <div style="clear: both;"><div style="float: right"><a href="" class="ctrl-media-connector-submit">&nbsp;</a><a class="ctrl-media-connector-cancel" >&nbsp;</a></div></div><br /></div>').prepend(popupText);
		var popup = titleCell.popup({
			el: popupIn, 
			dType: "leftTop",
			open: true,
			autoHide: false 
		});
		popupIn.find(".ctrl-media-connector-cancel").click(function(){
			popup.remove();
			return false;
		});
		popupIn.find(".ctrl-media-connector-submit").click(function(){
			ArrowDataConnector.saveObjectData(
					'media.MediaElement',
					elId,
					{"description": popupText.val()},
					{},
					function(data){
						CtrlAjax.changeStateVars(titleCell,{},{});
					}
			);
			popup.remove();
			return false;
		});
		popupText.focus();
		return false;
	});
	
	
	
	
	$(".ctrl-media-connector-file-delete", context).click(function(){
		var link = $(this);
		if(!confirm("Czy napewno usunąć element?"))
			return false;
		
		
		var conId = $(this).attr("href" );
		var elId = $(this).attr("rel" );
		
		ArrowDataConnector.getObjects(
			'media.MediaElementConnection', 
			{"criteria": "element_id:==:" + elId},
			{},
			function(data){ 
				var target = "connection";
				if(data.length == 1){
					if(confirm("Element nie jest używany nigdzie indziej, usunąć całkowicie?"))
						target = "element";
				}
				
				var model;
				var id;
				if(target == "connection"){
					model = "media.MediaElementConnection";
					id = conId;
				}else{
					model = "media.MediaElement";
					id = elId;
				}
				ArrowDataConnector.deleteObject(model,id, {}, function(){
					CtrlAjax.changeStateVars(link,{},{});
				});
				
			}
		);

		return false;
	});
	
	$(".ctrl-media-connector-add-local-show", context).click(function(){
		$(".ctrl-media-connector-add-bar", context).toggle();
		$(".ctrl-media-connector-add-bar input", context).focus();
		return false;
	}).tooltip();
	
		
	

	
	$(".ctrl-media-connector-add-chosen", context).click(function(){
		var link = $(this);
		
		// TODO gdy formularz obejmuje więcej mediaelementów context jest niepoprawny
		//var val = context.find(".ctrl-media-connector-add-bar input[type=hidden]").val();
		var val = link.prev().find("input[type=hidden]").val();
		
		if( val == "" )
			alert("Brak wybranych plików");
		else{
				ArrowDataConnector.createObject( "media.MediaElementConnection", {
					element_id: val,
					object_id: link.attr("rel"),
					model: link.attr("href"),
					name: link.attr("alt")
					
				}, {}, function(){
						CtrlAjax.changeStateVars(link,{},{});
					
				});
				
		}
			
		
		
		return false;
	}).tooltip();
	
	
	$(".ctrl-media-connector-file-icon-img", context).click(function(){
		var size = $(this).attr("size").split("x");
		var width = parseInt(size[0]);
		var height = parseInt(size[1]);
		if(width > $(window.top).width() - 25){
			width = $(window.top).width() - 25;
		}
		if(height > $(window.top).height() - 25){
			height = $(window.top).height() - 45;
		}
		
		//max-width: '+width+'px; max-height: '+height+'px;
		var img = '<div style="padding: 0;"><img src="' + $(this).attr("alt") + '" title="Podgląd obrazu" style="" /></div>';
		window.top.$(img).dialog({
			title: "Obraz",
			width: width+25,
			height: height+45,
			resizable: false,
			autoOpen: true
		});
		
		/*
		window.top.tmt.jquery.modalframe.open($(this).attr("alt"), {width: parseInt(size[0]), height: parseInt(size[1])+27, onOpen:function(){
			hideLoading();
			$(".ui-dialog").show();
		}});
		*/
	});
	
}